package com.ctg.behavior.calc.impala.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ctg.behavior.calc.common.CtPathNode;

public class CtCalcRowsUtils {

	public static String buildDetailResultRowKey(List<String> byValues) {
		StringBuilder sbuf = new StringBuilder();
		sbuf.append("row@");
		for (String oneByValue : byValues) {
			sbuf.append("@").append(oneByValue);
		}
		return sbuf.toString();
	}

	public static int findListIndex(List<String> listData, String indexData) {
		for (int index = 0; index < listData.size(); index++) {
			if (listData.get(index).equals(indexData)) {
				return index;
			}
		}
		return -1;
	}

	public static String buildPathNodeKey(CtPathNode calcNode) {

		if (calcNode.getColumnIndex() < 0) {
			return "";
		}

		StringBuilder sbuf = new StringBuilder();

		sbuf.append(calcNode.getColumnIndex());
		sbuf.append("-").append(calcNode.getEventId());
		if (!StringUtils.isEmpty(calcNode.getByProperty())) {
			sbuf.append("-").append(calcNode.getByProperty());
			sbuf.append("-").append(calcNode.getByValue());
		}
		sbuf.append("@").append(calcNode.getSeqNo());

		return sbuf.toString();
	}

	public static String fetchResultUserPathNodeKey(String pathNodeKey) {
		int index = pathNodeKey.indexOf("@");
		if (index <= 0) {
			return pathNodeKey;
		}
		return pathNodeKey.substring(0, index);
	}
}
